#include "mymath.h"

/***********************************************************************************************************************
 * Function Name: MidAvg_Filter
 * @brief  This function gets the middle average of middle values of the results.
 * @param  buf - the pointer of the conversion result
 * @param  num - set the numbers of ad conversion result，不小于4最好是10这样子
 * @return average
 ***********************************************************************************************************************/
fp32 MidAvg_Filter(fp32 *buf, uint8 num)
{
    uint8 i, j;
    fp32 tmp;
    fp32 sum;

    /* sort the value from small to large */
    for (i = 0; i < num; i++)
    {
        for (j = 0; j < ((num - 1) - i); j++)
        {
            if (buf[j] > buf[j + 1])
            {
                tmp = buf[j];
                buf[j] = buf[j + 1];
                buf[j + 1] = tmp;
            }
        }
    }

    /* Remove the smallest and largest values, then take the average */
    sum = 0;
    for (i = 2; i < (num - 2); i++)
    {
        sum += buf[i];
    }
    tmp = sum / (num - 4);

    return (tmp);
}
